Java Iterators, Comparable, and Comparators Quiz

Easy Level

1. Which interface must a class implement to define its own natural sorting order?

2. What is the main purpose of the `Iterator` interface?

3. Which method is NOT part of the `Iterator` interface?

4. If you want to sort a list of objects using a custom sorting logic without modifying the object's class, you should use:

5. The `compareTo()` method, from the `Comparable` interface, should return what value if the current object is "less than" the object being compared to?

6. Which method of the `Collections` class is used to sort a `List`?

7. The "for-each" loop in Java can be used on any object that implements which interface?

8. What exception is typically thrown if you call the `next()` method on an `Iterator` when there are no more elements?

9. The `compare(T o1, T o2)` method is the single abstract method of which interface?

10. In a standard `ArrayList`, what happens if you try to modify the list (e.g., add or remove an element) while iterating over it using a standard `Iterator`?

Medium Level

11. Consider a `Person` class with `name` and `age` fields. How would you create a `Comparator` to sort a list of `Person` objects by age in ascending order using a lambda expression?

12. What is the relationship between the `Iterable` and `Iterator` interfaces?

13. A class `Car` implements `Comparable` by comparing car prices. You have a `List` called `cars`. What will `Collections.sort(cars);` do?

14. Which static factory method in the `Comparator` interface is used to create a comparator from a function that extracts a `Comparable` sort key?

15. If a `Comparator` returns `0` for two objects, `o1` and `o2`, what does this imply?

16. How can you create a `Comparator` that applies a second sorting criterion if the first one results in a tie?

17. What is the purpose of the `Comparator.reversed()` method?

18. You have a `List`. Which code snippet correctly sorts the list by the length of the strings?

19. When is it more appropriate to use `Comparable` instead of `Comparator`?

20. What does the `Iterator.remove()` method do?

Hard Level

21. Which statement best describes a "fail-fast" iterator?

22. You have a class `Product` with `getCategory()` (String) and `getPrice()` (double) methods. How do you sort a list of `Product`s first by category alphabetically, and then by price in descending order?

23. What is a key difference between an `Iterator` and a `Spliterator`?

24. Consider a `TreeSet` which stores elements in sorted order. If you add objects of a class that does not implement `Comparable` and you don't provide a `Comparator` to the `TreeSet` constructor, what happens?

25. How would you create a `Comparator` for a `Person` class that sorts `null` `Person` objects to appear first, followed by non-null objects sorted by name?

26. The contract for `Comparator.compare(o1, o2)` requires that `sgn(compare(x, y)) == -sgn(compare(y, x))`. What property does this enforce?

27. Iterators for which of the following collections are typically "fail-safe" (or weakly consistent) and do not throw `ConcurrentModificationException`?

28. What is the purpose of the `forEachRemaining(Consumer action)` default method in the `Iterator` interface?

29. You implement `compareTo` in a way that `x.compareTo(y)` returns `0` but `x.equals(y)` returns `false`. If you add both `x` and `y` to a `TreeSet`, what will happen?

30. Given `Comparator comp = (s1, s2) -> s1.length() - s2.length();`. What is the result of sorting a list `["a", "cccc", "b"]` using `comp.thenComparing(Comparator.naturalOrder())`?